package com.weiaibenpao.demo.chislim.ui.activity.moment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.LikerListBean;
import com.weiaibenpao.demo.chislim.bean.Praise;
import com.weiaibenpao.demo.chislim.presenter.LikerListPresenter;
import com.weiaibenpao.demo.chislim.ui.activity.me.UserZoneActivity;
import com.weiaibenpao.demo.chislim.ui.adapter.LikerListAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.LikerListView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LikerListActivity extends BaseActivity<LikerListView,LikerListPresenter> implements LikerListView, BaseQuickAdapter.OnItemClickListener,BaseQuickAdapter.RequestLoadMoreListener {
    public static final String MOMENT_ID = "moment_id";
    private LikerListAdapter mAdapter;
    private int mNextPage =1;
    private int mMomentId;
    @BindView(R.id.rv_list)
    RecyclerView mLikerList;


    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_liker_list;
    }

    @Override
    public LikerListPresenter initPresenter() {
        return new LikerListPresenter();
    }

    @Override
    public void init(Bundle savedInstanceState) {
        if(getIntent() != null){
            mMomentId = getIntent().getIntExtra(MOMENT_ID,-1);
        }
        initView();
        mPresenter.getLikerList(mMomentId,mNextPage);
    }

    private void initView() {
        mAdapter = new LikerListAdapter();
        mAdapter.setOnLoadMoreListener(this,mLikerList);
        mAdapter.setOnItemClickListener(this);
        mLikerList.setLayoutManager(new GridLayoutManager(this,4));
        mLikerList.setAdapter(mAdapter);
    }

    public static void startSelf(Context context,int momentId){
        Intent intent = new Intent(context,LikerListActivity.class);
        intent.putExtra(MOMENT_ID,momentId);
        context.startActivity(intent);
    }

    @OnClick(R.id.left_image)
    public void onClick(View view){
        finish();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UserZoneActivity.startSelf(LikerListActivity.this,mAdapter.getData().get(position).getUserId());
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.getLikerList(mMomentId,mNextPage);
    }

    @Override
    public void onGetLikerListSuccess(LikerListBean bean) {
        setData(bean.getList());
        mNextPage++;
        Logger.e("onGetLikerListSuccess");
    }

    @Override
    public void onGetLikerListFailed(String errMsg) {
        Logger.e("onGetLikerListFailed "+errMsg);
    }

    private void setData(List<Praise> data) {
        final int size = data == null ? 0 : data.size();
        if (size > 0) {
            mAdapter.addData(data);
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
        }
    }
}
